package com.qiuzhiyi.dialogs;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.DatePicker;
import android.widget.TextView;

import com.li.utils.ScreenUtil;
import com.qiuzhiyi.R;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

/**
 * Author: Li
 * Date: 2017/11/14 0014
 * Description:
 */

public class DateDialog implements View.OnClickListener {

    private Context mContext;
    private TextView mTextTitle;
    private AlertDialog mDialog;
    private OnDateChooseListener mOnDateChooseListener;

    public DateDialog(Context context, OnDateChooseListener onDateChooseListener) {
        mContext = context;
        mOnDateChooseListener = onDateChooseListener;
        initDialog();
    }

    private void initDialog() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_date, null);
        DatePicker datePicker = view.findViewById(R.id.date_picker);
        mTextTitle = view.findViewById(R.id.txt_title);
        TextView textConfirm = view.findViewById(R.id.txt_confirm);
        textConfirm.setOnClickListener(this);
        showDate(datePicker, mTextTitle);
        datePicker.init(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                showDate(view, mTextTitle);
            }
        });

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext)
                .setView(view);
        mDialog = builder.create();
    }

    public void show() {
        mDialog.show();
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(
                    ContextCompat.getColor(mContext, R.color.transparent)));
            window.getDecorView().setPadding((int) (ScreenUtil.getScreenWidth(mContext) * 0.1f),
                    0, (int) (ScreenUtil.getScreenWidth(mContext) * 0.1f), 0);
        }
    }

    private void showDate(DatePicker datePicker, TextView textView) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());

        DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        String date = format.format(calendar.getTime());
//        String[] week = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
//        String content = date + week[calendar.get(Calendar.DAY_OF_WEEK) - 1];
//        textView.setText(content);
        textView.setText(date);
    }

    @Override
    public void onClick(View v) {
        mOnDateChooseListener.onDateChoose(mTextTitle.getText().toString());
        mDialog.dismiss();
    }

    public interface OnDateChooseListener {
        void onDateChoose(String date);
    }
}
